define(function(require){
    var main =require('/static/main.js');
    require('/static/ajaxfileupload.js');
    var m = new main();
    function Cont(){
        this.Fun._init();
    }
    Cont.prototype.Fun = {
        TagIds:[],
        _init:function(){
            this.getlist();
            this.bindEvent();
        },
        bindEvent:function(){

        },
        getlist:function(){
            var param = {};
            var table1 = $('#datatable1').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    param.AdvertorialStatus = 100;
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/promote/get-my-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            callback(returnData);
                        }
                    });
                },
                "aoColumns":[
                    {"mDataProp": 'Title'},
                    {
                        "mDataProp": 'PublishTime',
                        render:function(data){
                            var time = m.formatTimeAll2(data);
                            return time;
                        }
                    },
                    {
                        "mDataProp": 'AdType',
                        render:function(data){
                            var AdType = '';
                            switch (data){
                                case 100: AdType = '销售软文';break;
                                case 200: AdType = '预约软文';break;
                                case 300: AdType = '报名软文';break;
                            }
                            return AdType;
                        }
                    },
                    {
                        "mDataProp": "AdvertorialId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            // console.log(arguments);
                            $(nTd).html('<span class="getDetail" data-edit="0" data-type="'+oData.AdType+'" data-id="'+sData+'" data-temptype="'+oData.TemplateType+'" >查看</span>');

                        }
                    }
                ],
            }).api();

            var table2 = $('#datatable2').dataTable({
                'ordering':false,
                "serverSide": true,
                "searching":false,
                'bLengthChange': false,
                'bPaginate': true,
                "bDestory": true,
                "bRetrieve": true,
                "bFilter":false,
                "bSort": false,
                "bProcessing": true,
                "autoWidth": false,
                ajax: function (data, callback, settings) {
                    //封装请求参数
                    param.PageSize = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                    param.start = data.start;//开始的记录序号
                    param.PageNo = (data.start / data.length)+1;//当前页码
                    param.AdvertorialStatus = 200;
                    param.Title = '';
                    //ajax请求数据
                    $.ajax({
                        type: "POST",
                        url: "/promote/get-my-list",
                        cache: false,  //禁用缓存
                        data: param,  //传入组装的参数
                        dataType: "json",
                        success: function (result) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = result.total;
                            returnData.recordsFiltered = result.total;
                            returnData.data = result.data;
                            callback(returnData);
                        }
                    });
                },

                "aoColumns":[
                    {"mDataProp": 'Title'},
                    {
                        "mDataProp": 'CreateTime',
                        render:function(data){
                            var time = m.formatTimeAll2(data);
                            return time;
                        }
                    },
                    {
                        "mDataProp": 'AdType',
                        render:function(data){
                            var AdType = '';
                            switch (data){
                                case 100: AdType = '销售软文';break;
                                case 200: AdType = '预约软文';break;
                                case 300: AdType = '报名软文';break;
                            }
                            return AdType;
                        }
                    },
                    {
                        "mDataProp": "AdvertorialId",
                        "sClass": "option_btn",
                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                            $(nTd).html('<span class="getDetail" data-edit="0" data-type="'+oData.AdType+'" data-temptype="'+oData.TemplateType+'" data-id="'+sData+'">查看</span>\
			                	            <span class="getDetail" data-edit="1" data-type="'+oData.AdType+'" data-temptype="'+oData.TemplateType+'" data-id="'+sData+'">编辑</span>\
                                            <span class="PublishBtn"  data-id="'+sData+'">发布</span>\
                                            <span class="DelBtn"  data-id="'+sData+'">删除</span>');

                        }
                    }
                ],
            }).api();

            // 点击tab
            $('.nav.nav-tabs li a').click(function() {
                var index  = $(this).parent().index();
                $(".tab-content .tab-pane").eq(index).show().siblings().hide();
            });

            // 点击搜索
            $('#btnSearch').click(function(){
                param.Title = $('.Title').val();
                param.AdType = $('.AdType').val();
                var type = parseInt($('.nav.nav-tabs .active a').attr('data-type'));
                if(type == 100){
                    var table = $('#datatable1').DataTable();
                    table.ajax.reload();
                }else if(type == 200){
                    var table = $('#datatable2').DataTable();
                    table.ajax.reload();
                }
            })
            // 点击重置按钮
            $('#btnRefresh').click(function(){
                $('.AdType option').each(function(){
                    if(parseInt($(this).val()) == ''){
                        $(this).attr('selected',true);
                    }else{
                        $(this).attr('selected',false);
                    }
                })
                param.Title = $('.Title').val('');
            })

            // 点击查看详情
            $(document).on('click','.getDetail',function(){
                var id = $(this).attr('data-id');
                var type = $(this).attr('data-type');
                var edit = $(this).attr('data-edit');
                var actionstr = '';
                if(type == 100 ){
                    actionstr = 'sales';
                }else if(type == 200 ){
                    var temptype = $(this).attr('data-temptype');
                    if(temptype == 100){
                        actionstr = 'reservation';
                    }else if(temptype == 300){
                        actionstr = 'reservation1';
                    }else if(temptype == 200){
                        actionstr = 'reservation2';
                    }else{
                        actionstr = 'reservation';
                    }
                }else if(type == 300){
                    actionstr = 'activity';
                }
                var str = '/promote/'+actionstr+'?advertorialid='+id;
                if(edit == 0){
                    str +='&islook=true';
                }
                window.location.href = str;
            })
            // 发布
            var isactive = false;
            $(document).on('click','.PublishBtn',function(){
                if(isactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                isactive = true;
                $.ajax({
                    type: "POST",
                    url: "/promote/ad-publish",
                    data :{AdvertorialId:id},
                    dataType: "json",
                    success: function (ret){
                        isactive = false;
                        if(ret.IsBizSuccess){
                            layer.msg("发布成功");
                            var table1 = $('#datatable1').DataTable();
                            table1.ajax.reload();
                            var table2 = $('#datatable2').DataTable();
                            table2.ajax.reload();
                        }else{
                            layer.msg("发布失败");
                        }
                    }
                });
            })
            // 删除
            var delactive = false;
            $(document).on('click','.DelBtn',function(){
                if(delactive == true){
                    return false;
                }
                var id = $(this).attr('data-id');
                delactive = true;
                $.ajax({
                    type: "POST",
                    url: "/promote/ad-del",
                    data :{AdvertorialId:id},
                    dataType: "json",
                    success: function (ret){
                        delactive = false;
                        if(ret.IsBizSuccess){
                            layer.msg("删除成功");
                            var table2 = $('#datatable2').DataTable();
                            table2.ajax.reload();
                        }else{
                            layer.msg("删除失败失败");
                        }
                    }
                });
            })
        }
    }
    var a = new Cont();

})